// pages/my/index.js
const app = getApp();

Page({
  getUserInfo: function (e) {
    console.log(e);
    let that = this;

    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    });

  },

  go2where(e) {
    // 私教
    if (this.data.user.roleId == 2)
      wx.redirectTo({
        url: '../teacher/index'
      });

    // 馆主
    if (this.data.user.roleId == 3)
      wx.redirectTo({
        url: '../owner/index'
      });

  },

  go2MemberShip(e) {
    wx.navigateTo({
      url: '../memberShip/index'
    });
  },

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    user: null,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    hasUserInfo: false,
  },

  formSubmit1(e) {
    let that = this;

    if (!that.data.hasUserInfo) {
      wx.showToast({
        title: '请点击上面的授权获取用户信息，按钮！',
        icon: 'none',
        duration: 2000
      });

      return;
    }
        
    this.setData({
      ['user.realName']: e.detail.value.realName,
      ['user.mobile']: e.detail.value.mobile,
      ['user.city']: that.data.userInfo.city,
      ['user.gender']: that.data.userInfo.gender,
      ['user.nickName']: that.data.userInfo.nickName,
      ['user.province']: that.data.userInfo.province,
      ['user.avatarUrl']: that.data.userInfo.avatarUrl,
      // ['user.roleId']: null,
    });

    wx.request({
      url: app.globalData.serverUrl + "/user/editUser",
      header: {
        token: app.globalData.token
      },
      method: "POST",
      data: this.data.user,
      success(resp) {
        console.log(resp);

        if (resp.data.success) {
          app.globalData.sysUser = that.data.user;
          
          wx.showToast({
            title: '操作成功',
            icon: 'none',
            duration: 2000
          });

          wx.removeStorage({
            key: 'userId',
            success: function(res) {
              console.log(res);
            } 
          });
          
        } else {
          wx.showToast({
            title: '操作失败',
            icon: 'none',
            duration: 2000
          });
        }

      },
      fail(resp) {
        console.error(resp);
      }
    });

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // wx.setNavigationBarTitle({
    //   title: app.globalData.parentEnterpriseName == null ? "" : app.globalData.parentEnterpriseName
    // });

    this.setData({
      user: app.globalData.sysUser
    });

    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      });

    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        });
      }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    return {
      title: "约课",
      imageUrl: "https://cw1.tw/CH/images/channel_master/af164017-3505-4e04-b1b3-bb45cd10cb94.jpg",
      path: "/pages/index/index?enterpriseId=" + app.globalData.enterpriseId,
      success: function (res) {
        var shareTickets = res.shareTickets;
        if (shareTickets.length == 0) {
          return false;
        }
        wx.getShareInfo({
          shareTicket: shareTickets[0],
          success: function (res) {
            var encryptedData = res.encryptedData;
            var iv = res.iv;
          }
        })
      },
      fail: function (res) {
        // 转发失败
        console.log(res);
      }
    }
  },
})